import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pdf_render/pdf_render_widgets.dart';
import 'package:share_plus/share_plus.dart';

import '../../base/MyScaffold.dart';
import '../../config/Constant.dart';
import '../../model/Bus.dart';
import '../../model/PDFFile.dart';
import '../../res.dart';
import '../../sqflite/CreateTableSqls.dart';
import '../../sqflite/DBUtil.dart';
import '../../utils/EventBus/EventBus.dart';
import '../../utils/Toast/MyTosat.dart';
import '../../utils/color/ColorUtils.dart';
import '../../widgets/MyImage.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';

class Selected extends StatefulWidget {
  const Selected({Key? key}) : super(key: key);

  @override
  State<Selected> createState() => _SelectedState();
}

class _SelectedState extends State<Selected> {
  late List<PDFFile> list = [];
  late bool All = true;
  late DBUtil dbUtil;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    List<PDFFile> clone = Get.arguments;
    list = List.from(clone);
    dbUtil = DBUtil();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    dbUtil.close();
  }

  setDelete() {
    list.removeWhere((element) => element.select);
    setState(() {});
  }

  delete(PDFFile pdfFile) async {
    await dbUtil!.open();
    await dbUtil
        ?.deleteByHelper(file, whereStr: 'id=?', whereArgs: [pdfFile.id]);
  }

  @override
  Widget build(BuildContext context) {
    return MyScaffold(
      title: "selected",
      trailingWidget: TextView(
        All ? "All" : "Cancel",
        height: 24,
        width: 58,
        alignment: Alignment.center,
        style: TextStyle(color: rgba(255, 78, 77, 1)),
        borderColor: rgba(255, 78, 77, 1),
        margin: const EdgeInsets.only(right: 16),
        radius: BorderRadius.circular(4),
        borderWidth: 1,
        onclick: () {
          if (All) {
            for (PDFFile d in list) {
              d.select = true;
            }
            All = false;
          } else {
            for (PDFFile d in list) {
              d.select = false;
            }
            All = true;
          }

          setState(() {});
        },
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemBuilder: (c, index) {
                return Container(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  height: 88,
                  child: Row(
                    children: [
                      Container(
                        margin: const EdgeInsets.only(right: 12),
                        width: 56,
                        height: 56,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(6),
                            border: Border.all(
                                color: rgba(230, 231, 235, 1), width: 0.5)),
                        alignment: Alignment.center,
                        child: PdfDocumentLoader.openFile(
                          list[index].path,
                          pageNumber: 1,
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Expanded(
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        TextView(
                                          list[index].name,
                                          overflow: TextOverflow.clip,
                                          maxLines: 2,
                                          style: TextStyle(
                                              color: ColorUtils.text,
                                              fontSize: 14),
                                        ),
                                        Container(
                                          margin:
                                              const EdgeInsets.only(top: 10),
                                          child: Row(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.center,
                                            children: [
                                              TextView(
                                                  list[index].time +
                                                      "  " +
                                                      list[index].size,
                                                  alignment: Alignment.center,
                                                  textAlign: TextAlign.center,
                                                  margin: const EdgeInsets.only(
                                                      right: 10),
                                                  style: TextStyle(
                                                      color: ColorUtils.text_,
                                                      fontSize: 10)),
                                              if (list[index].star)
                                                const MyLocalImage(
                                                  Res.sys_icon_file_star,
                                                  width: 12,
                                                  height: 12,
                                                )
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  MyLocalImage(
                                    list[index].select
                                        ? Res.sys_icon_file_plane_sel
                                        : Res.sys_icon_file_plane_nor_black,
                                    width: 24,
                                    height: 24,
                                    onclick: () {
                                      list[index].select = !list[index].select;
                                      print(list.toString());
                                      setState(() {});
                                    },
                                  ),
                                ],
                              ),
                            ),
                            const Divider(
                              height: 1,
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                );
              },
              itemCount: list.length,
            ),
          ),
          const Divider(
            height: 1,
          ),
          Container(
            height: 48,
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Row(
                  children: [
                    const MyLocalImage(
                      Res.snackbar_icon_share,
                      width: 24,
                      height: 24,
                    ),
                    TextView(
                      "Share",
                      margin: const EdgeInsets.only(left: 10),
                      style: TextStyle(color: ColorUtils.text, fontSize: 16),
                      onclick: () {
                        List<PDFFile> result =
                            list.where((item) => item.select).toList();
                        int results = result.length;
                        if (results == 0) {
                          MyToast()
                              .show("Please select a file", icon_tips: false);
                          return;
                        }
                        final files = <XFile>[];

                        for (PDFFile item in result) {
                          files.add(XFile(item.path));
                        }
                        Share.shareXFiles(files, text: "multifile");
                      },
                    )
                  ],
                ),
                Container(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 55, vertical: 16),
                  child: const VerticalDivider(),
                ),
                InkWell(
                  onTap: () {
                    List<PDFFile> result =
                        list.where((item) => item.select).toList();
                    int results = result.length;
                    if (results == 0) {
                      MyToast().show("Please select a file", icon_tips: false);
                      return;
                    }
                    Get.defaultDialog(
                      radius: 10,
                      contentPadding: EdgeInsets.zero,
                      titlePadding: const EdgeInsets.only(top: 20),
                      content: TextView(
                        "Are you sure you want to delete the ($results) file?",
                        maxLines: 2,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            color: ColorUtils.text,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                        margin: const EdgeInsets.symmetric(
                            horizontal: 32, vertical: 16),
                      ),
                      actions: [
                        Column(
                          children: [
                            const Divider(
                              height: 1,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                TextView(
                                  "CANCLE".tr,
                                  style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      color: Colors.black,
                                      fontSize: 16),
                                  backcolor: rgba(247, 247, 247, 1),
                                  textAlign: TextAlign.center,
                                  radius: BorderRadius.circular(6),
                                  padding:
                                      const EdgeInsets.symmetric(vertical: 11),
                                  width: 140,
                                  margin:
                                      const EdgeInsets.symmetric(vertical: 16),
                                  onclick: () {
                                    Get.back();
                                  },
                                ),
                                const SizedBox(
                                  width: 16,
                                ),
                                TextView(
                                  "Delete".tr,
                                  style: const TextStyle(
                                      fontWeight: FontWeight.bold,
                                      color: Colors.white,
                                      fontSize: 16),
                                  backcolor: rgba(255, 78, 77, 1),
                                  textAlign: TextAlign.center,
                                  radius: BorderRadius.circular(6),
                                  padding:
                                      const EdgeInsets.symmetric(vertical: 11),
                                  width: 140,
                                  margin:
                                      const EdgeInsets.symmetric(vertical: 16),
                                  onclick: () {
                                    Get.back();
                                    for (PDFFile file in result) {
                                      if (file.select) {
                                        // delete(file);
                                        EventBusUtil.fireEvent(
                                            Bus(Constant.DELETE_MSG, file));
                                      }
                                    }
                                    setDelete();
                                  },
                                ),
                              ],
                            ),
                          ],
                        )
                      ],
                    );
                  },
                  child: Row(
                    children: [
                      const MyLocalImage(
                        Res.snackbar_icon_delect,
                        width: 24,
                        height: 24,
                      ),
                      TextView(
                        "Delete",
                        margin: const EdgeInsets.only(left: 10),
                        style:
                            TextStyle(color: rgba(255, 0, 14, 1), fontSize: 16),
                      )
                    ],
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
